package cn.itcast.service.impl;

import cn.itcast.listener.StudentDataListener;
import cn.itcast.mapper.StudentsMapper;
import cn.itcast.pojo.PageBean;
import cn.itcast.pojo.StudentGrade;
import cn.itcast.pojo.Students;
import cn.itcast.service.StudentsService;

import com.alibaba.excel.EasyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class StudentsServiceImpl implements StudentsService {
    @Autowired
    private StudentsMapper studentsMapper;
    /*@Override
    public PageBean<Students> findAllColStudents(String name, Integer degree, Integer classId, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Students> students = studentsMapper.findAllColStudents(name, degree, classId, page, pageSize);
        Page<Students> p = (Page<Students>) students;
        PageBean<Students> pageBean = new PageBean<>((int) p.getTotal(), students);
        return pageBean;
    }*/

    @Override
    public PageBean<Students> findAllColStudents(String name, Integer degree, Integer classId, Integer page, Integer pageSize) {
        return null;
    }

    @Override
    public boolean deleteById(ArrayList< Integer> ids) {
        int row = studentsMapper.deleteById(ids);
        return row > 0;
    }

    @Override
    public boolean addStudents(Students students) {
        students.setViolationCount(0);
        students.setViolationScore(0);
        int row = studentsMapper.addStudents(students);
        return row > 0;
    }

    @Override
    public Students findStudentsById(Integer id) {
        Students students = studentsMapper.findStudentsById(id);

        return students;
    }

    @Override
    public boolean updateStudents(Students students) {
        int row = studentsMapper.updateStudents(students);
        return row > 0;
    }

    @Override
    public boolean discipline(Integer id, Integer score) {
        int row = studentsMapper.discipline(id, score);
        return row > 0;
    }
    @Override
    public List<StudentGrade> studentDegreeData() {
        List<StudentGrade> list = studentsMapper.studentDegreeData();
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveStudent(Students student) {
        studentsMapper.insert(student);
    }

    @Override
    public void importStudents(String filePath) {
        EasyExcel.read(filePath, Students.class, new StudentDataListener(this))
                .sheet()
                .doRead();
    }

    @Override
    public void exportStudents() {
        // 写法2
        String fileName = "simpleWrite" + System.currentTimeMillis() + ".xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
        //查询所有学生数据
        List<Students> students = studentsMapper.selectList(null);
        EasyExcel.write(fileName, Students.class).sheet("模板").doWrite(students);
    }
}
